package com.zygh.lz.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zygh.lz.dao.IndexMapper;
import com.zygh.lz.dao.StaffMapper;
import com.zygh.lz.dao.XlevelserviceMapper;
import com.zygh.lz.entity.Sptype;
import com.zygh.lz.entity.Staff;
import com.zygh.lz.entity.Xarea;
import com.zygh.lz.entity.Xlevelservice;
import com.zygh.lz.service.IndexService;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.util.JwtUtils;
import com.zygh.lz.util.TreeUtil;
import com.zygh.lz.vo.ResultBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wxh
 */
@Service
public class IndexServiceImpl implements IndexService {
    @Resource
    private IndexMapper indexMapper;
    @Resource
    private JwtUtils jwtUtils;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private XlevelserviceMapper xlevelserviceMapper;
    @Resource
    private TreeUtil treeUtil;
    @Override
    public List<Map<String, Object>> station(HttpServletRequest request) {
        List<Map<String, Object>> station = indexMapper.station();
        //加权限
        Integer sectionId = getSectionId(request);
        for (Map<String, Object> stringObjectMap : station) {
            String name = String.valueOf(stringObjectMap.get("station"));
            List<Map<String, Object>> battalion = indexMapper.battalion(name,sectionId);
            stringObjectMap.put("child",battalion);
        }

        return station;
    }

    @Override
    public Map<String, Object> detail(Xarea xarea) {
        Map<String,Object> data = new HashMap<>();
        String today = DateUtil.today();
        //部署岗位
        Integer stationCount = indexMapper.stationCount(xarea);
        // 已经签到岗位
        Integer signStationCount = indexMapper.signStationCount(xarea,today);
       // 部署警力
        Integer policeNum = indexMapper.policeNum(xarea);
        // 已签到警力
        Integer signPoliceNum = indexMapper.signPoliceNum(xarea, today);

        List<Map<String, Object>> maps = indexMapper.dataArr(xarea);
        for (Map<String, Object> map : maps) {
            if(Objects.nonNull(map)&&Objects.nonNull(map.get("id"))){
                String id = map.get("id").toString();
                List<Map<String,Object>> detail = indexMapper.detail(id,today);
                map.put("detail",detail);
            }

        }
        data.put("stationCount",stationCount);
        data.put("signStationCount",signStationCount);
        data.put("policeNum",policeNum);
        data.put("signPoliceNum",signPoliceNum);
        data.put("dataArr",maps);
        if(CollUtil.isEmpty(maps)||Objects.isNull(maps.get(0))){
            data.put("dataArr",new ArrayList<>());
        }
        return data;
    }

    @Override
    public List<Map<String, Object>> battalion(HttpServletRequest request) {
        Integer sectionId = getSectionId(request);
        List<Map<String, Object>> maps = indexMapper.indexBattalion(sectionId);
        for (Map<String, Object> map : maps) {
            String name = String.valueOf(map.get("battalion"));

            List<Map<String, Object>> indexStation = indexMapper.indexStation(name);
            map.put("child",indexStation);
        }
        return maps;
    }

    @Override
    public List<Map> sptype(HttpServletRequest request) {
        List<Map<String, Object>> sptypes = indexMapper.sptype();

        sptypes.forEach(x -> {
            List<Xlevelservice> xlevelservices = xlevelserviceMapper.selectList(Wrappers.<Xlevelservice>lambdaQuery()
                    .eq(Xlevelservice::getPositionId, x.get("id"))
                    .eq(Xlevelservice::getZt,1));
            x.put("people", 0);
            x.put("count", 0);

            if (CollUtil.isNotEmpty(xlevelservices)) {
                int sum = xlevelservices.stream().filter(y -> Objects.nonNull(y.getNumber())).mapToInt(Xlevelservice::getNumber).sum();
                x.put("people", sum);
                x.put("count", xlevelservices.size());
            }
        });

        List<Map> maps = treeUtil.buildTreePolice(sptypes);
        return maps;
    }

    @Override
    public ResultBean sptypeDetail(Sptype sptype) {
        Map da = new HashMap();

        //岗位详情
        List<Map> data = xlevelserviceMapper.sptypeDetail(sptype.getId());
        //部署警力
        int sum = data.stream().filter(x -> Objects.nonNull(x.get("number"))).mapToInt(x -> Integer.valueOf(x.get("number").toString())).sum();
        da.put("deployPolice",sum);
        //已签警力
        AtomicInteger signPolice = new AtomicInteger();
        //已签岗位
        AtomicInteger signPosts = new AtomicInteger();
        data.forEach(x->{
            //岗位详情下签到警力列表
            List<Map> signData = indexMapper.sptypeDetail(x.get("id").toString(),DateUtil.today());

            Integer signPost = indexMapper.sign(x.get("id").toString(),DateUtil.today());
            //已经签到岗位
            x.put("signPost",signPost);
            x.put("signData",signData);
            if(Objects.nonNull(signPost)){
                signPosts.addAndGet(signPost);
            }

            signPolice.addAndGet(signData.size());
        });
        da.put("signPolice",signPolice);
        da.put("data",data);
        //部署岗位数
        da.put("postNum",data.size());
        //已经签到岗位
        da.put("signPosts",signPosts);
         return ResultUtil.setOK("查询成功",da);
    }

    //获取部门id
    private Integer getSectionId(HttpServletRequest request) {
        String token = request.getHeader(jwtUtils.getHeader());

        String userName = jwtUtils.getClaimByToken(token).getSubject();
        Staff staff = staffMapper.selectOne(Wrappers.<Staff>lambdaQuery().eq(Staff::getStaffTel, userName));
        Integer sectionDid = null;
        if (Objects.nonNull(staff)) {
            sectionDid = staff.getSysSectionId();
            if (staff.getSysSectionId() == 74) {
                sectionDid = null;
            }
        }
        return sectionDid;
    }
}
